var express = require('express');
const {
  json
} = require('express/lib/response');
var router = express.Router();
var db = require('../model/index')
const dateFn = require('../public/package')

/* 查询所有轮播 */
router.get('/operate/getallbanner', function (req, res, next) {
  let mysql = 'SELECT * FROM operate_banner';
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询轮播失败，请稍后重试'
      })
    }
    //筛选数据
    if (JSON.stringify(req.query) != '{}') {
      for (let i = 0; i < data.length; i++) {
        let shelfTime = data[i].shelfTime.split('~')
        let startTime = Date.parse(shelfTime[0].replace('-', '/'));
        let endTime = Date.parse(shelfTime[1].replace('-', '/'));
        if (data[i].title.indexOf(req.query.title) == -1) {
          data.splice(i, 1);
          i--;
        } else if (req.query.shelf != '全部') {
          if (data[i].shelf != req.query.shelf) {
            data.splice(i, 1);
            i--;
          };
        } else if (req.query.shelfTime) {
          let reqTime = Date.parse(req.query.shelfTime.replace('-', '/'));
          if (reqTime < startTime || reqTime > endTime) {
            data.splice(i, 1);
            i--;
          }
        }
      }
    };
    res.json({
      code: 200,
      msg: '查询轮播成功',
      data: data
    })
  })
});


/* 查询所有专区 */
router.get('/operate/getallarea', function (req, res, next) {
  let mysql = `SELECT * FROM operate_area`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询专区失败，请稍后重试'
      })
    }
    //筛选数据
    console.log(data[0])
    if (JSON.stringify(req.query) != '{}') {
      for (let i = 0; i < data.length; i++) {
        let shelfTime = data[i].shelfTime.split('~')
        let startTime = Date.parse(shelfTime[0].replace('-', '/'));
        let endTime = Date.parse(shelfTime[1].replace('-', '/'));
        if (data[i].title.indexOf(req.query.title) == -1) {
          data.splice(i, 1);
          i--;
        } else if (req.query.shelf != '全部') {
          if (data[i].shelf != req.query.shelf) {
            data.splice(i, 1);
            i--;
          };
        } else if (req.query.shelfTime) {
          let reqTime = Date.parse(req.query.shelfTime.replace('-', '/'));
          if (reqTime < startTime || reqTime > endTime) {
            data.splice(i, 1);
            i--;
          }
        }
      }
    };
    res.json({
      code: 200,
      msg: '查询专区成功',
      data: data
    })
  })
});


/* 查询所有场景 */
router.get('/operate/getallscene', function (req, res, next) {
  let mysql = `SELECT * FROM operate_scene`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询场景失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '查询场景成功',
      data: data
    })
  })
});

/* 查询所有关键词 */
router.get('/operate/getallsearch', function (req, res, next) {
  let mysql = `SELECT * FROM operate_search`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询关键词 失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '查询关键词成功',
      data: data
    })
  })
});


/* 查询所有标签 */
router.get('/operate/getalllabel', function (req, res, next) {
  let mysql = `SELECT * FROM operate_label`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询标签失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '查询标签成功',
      data: data
    })
  })
});

/* 查询所有运费 */
router.get('/operate/getallfreight', function (req, res, next) {
  let mysql = `SELECT * FROM operate_freight`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询运费失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '查询运费成功',
      data: data
    })
  })
});


/*添加*/
//banner
router.post('/operate/addBanner', function (req, res, next) {
  console.log(req.body)
  let mysql = `INSERT INTO operate_banner VALUES (NULL,'${req.body.title}','${req.body.img}','${req.body.address}','${req.body.delivery}','${req.body.shelfTime}','${req.body.time}','${req.body.genre}');`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '添加失败，请稍后再试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '添加成功',
      data: data
    })
  })
});
/*添加operate_freight运费 */
//freight
router.post('/operate/addFreight', function (req, res, next) {
  let mysql = `INSERT INTO operate_freight  VALUES (NULL,'${req.body.area}','${req.body.freight}');`
  // console.log(req.body);
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '添加失败，请稍后再试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '添加成功',
      data: data
    })
  })
});
/*添加operate_label 标签*/
router.post('/operate/addLabel', function (req, res, next) {
  let mysql = `INSERT INTO operate_label  VALUES (NULL,'${req.body.title}','${req.body.address}','${req.body.shelf}');`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '添加失败，请稍后再试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '添加成功',
      data: data
    })
  })
});
/*添加场景管理 */
// operate_scene
router.post('/operate/addScene', function (req, res, next) {
  console.log(req.body)
  if (req.body.shelf) {
    req.body.shelf = '是'
  } else {
    req.body.shelf = '否'
  }
  let addTime = dateFn();
  let mysql = `INSERT INTO operate_scene  VALUES (NULL,'${req.body.title}','${req.body.img}','${req.body.address}','${req.body.shelf}','${addTime}');`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '添加失败，请稍后再试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '添加成功',
      data: data
    })
  })
})
/*添加搜索 */
//operate_search
router.post('/operate/addSearch', function (req, res, next) {
  if (req.body.shelf) {
    req.body.shelf = '是'
  } else {
    req.body.shelf = '否'
  }
  let mysql = `INSERT INTO operate_search  VALUES (NULL,'${req.body.title}','${req.body.address}','${req.body.shelf}');`
  console.log(req.body);
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '添加失败，请稍后再试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '添加成功',
      data: data
    })
  })
});
/*添加专区*/
//operate_area
router.post('/operate/addSpecial', function (req, res, next) {
  let mysql = `INSERT INTO operate_area VALUES (NULL,'${req.body.title}','${req.body.model}','${req.body.address}','${req.body.shelf}','${req.body.shelfTime}','${req.body.time}');`
  console.log(req.body);
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '添加失败，请稍后再试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '添加成功',
      data: data
    })
  })
});


/*是否上架轮播数据*/
// operate_banner
router.post('/operate/bannerShelf', function (req, res, next) {
  let mysql = `UPDATE operate_banner SET shelf='${req.body.shelf}' 
  WHERE id='${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '商品上架失败,请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '商品上架',
      data: data
    })
  })
});

/*是否上架数据*/
// operate_area
router.post('/operate/specialShelf', function (req, res, next) {
  let mysql = `UPDATE operate_area SET shelf='${req.body.shelf}' 
  WHERE id='${req.body.id}';`
  console.log(req.body)
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '商品上架失败,请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '商品上架',
      data: data
    })
  })
});
/*是否上架传数据*/
// /operate/searchShelf
router.post('/operate/searchShelf', function (req, res, next) {
  let mysql = `UPDATE operate_search SET shelf='${req.body.shelf}' 
  WHERE id='${req.body.id}';`
  console.log(req.body)
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '商品上架失败,请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '商品上架',
      data: data
    })
  })
});
// 
/*是否上架传数据*/
// operate_label
router.post('/operate/labelShelf', function (req, res, next) {
  let mysql = `UPDATE operate_label SET shelf='${req.body.shelf}' 
  WHERE id='${req.body.id}';`
  console.log(req.body)
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '商品上架失败,请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '商品上架',
      data: data
    })
  })
});
/*是否上架传数据*/
// operate_scene
router.post('/operate/sceneShelf', function (req, res, next) {
  let mysql = `UPDATE operate_scene SET shelf='${req.body.shelf}' 
  WHERE id='${req.body.id}';`
  console.log(req.body)
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '商品上架失败,请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '商品上架',
      data: data
    })
  })
});



/*删除轮播信息  */
// operate_banner
router.post('/oprate/delBanner', function (req, res, next) {
  let mysql = `
  DELETE FROM operate_banner WHERE id='${req.body.id}';
  `
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '删除失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '删除成功',
      data: data
    })
  })
});
/*删除信息  */
// operate_freight
router.post('/oprate/delFreight', function (req, res, next) {
  let mysql = `
  DELETE FROM operate_freight WHERE id='${req.body.id}';
  `
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '删除失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '删除成功',
      data: data
    })
  })
});

/*删除信息  */
// operate_label
router.post('/oprate/delLabel', function (req, res, next) {
  let mysql = `
  DELETE FROM operate_label WHERE id='${req.body.id}';
  `
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '删除失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '删除成功',
      data: data
    })
  })
});
/*删除信息  */
// operate_scene
router.post('/oprate/delScene', function (req, res, next) {
  let mysql = `
  DELETE FROM operate_scene WHERE id='${req.body.id}';
  `
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '删除失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '删除成功',
      data: data
    })
  })
});
/*删除信息  */
// operate_search
router.post('/oprate/delSearch', function (req, res, next) {
  let mysql = `
  DELETE FROM operate_search WHERE id='${req.body.id}';
  `
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '删除失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '删除成功',
      data: data
    })
  })
});
/*删除信息  */
// operate_area
router.post('/oprate/delSpecial', function (req, res, next) {
  let mysql = `
  DELETE FROM operate_area WHERE id='${req.body.id}';
  `
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '删除失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '删除成功',
      data: data
    })
  })
});


/* 编辑轮播信息 */
//editBanner
router.post('/oparate/editBanner', function (req, res, next) {
  console.log(req.body)
  if (req.body.shelf) {
    req.body.shelf = '是'
  } else {
    req.body.shelf = '否'
  }
  let mysql = `UPDATE operate_banner SET title='${req.body.title}',img='${req.body.img}',address='${req.body.address}',shelf='${req.body.shelf}',shelfTime='${req.body.shelfTime}',time='${req.body.time}',genre='${req.body.genre}' WHERE id=${req.body.id};`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '编辑失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '编辑成功',
      data: data
    })
  })
});

/* 编辑信息标签 */
//editLabel
router.post('/oparate/editLabel', function (req, res, next) {
  console.log(req.body)
  if (req.body.shelf) {
    req.body.shelf = '是'
  } else {
    req.body.shelf = '否'
  }
  let mysql = `UPDATE operate_label SET title='${req.body.title}',address='${req.body.address}',shelf='${req.body.shelf}' where id=${req.body.id};`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '编辑失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '编辑成功',
      data: data
    })
  })
});

/* 编辑信息 */
//operate_area
router.post('/oparate/editSpecial', function (req, res, next) {
  console.log(req.body)
  if (req.body.shelf) {
    req.body.shelf = '是'
  } else {
    req.body.shelf = '否'
  }
  let mysql = `UPDATE operate_area SET title='${req.body.title}',model='${req.body.model}',address='${req.body.address}',shelf='${req.body.shelf}',shelfTime='${req.body.shelfTime}',time='${req.body.time}' WHERE id='${req.body.id}';
  `
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '编辑失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '编辑成功',
      data: data
    })
  })
});
/* 编辑信息 */
//operate_freight
router.post('/oparate/editFreight', function (req, res, next) {
  let mysql = `
  UPDATE operate_freight SET area='${req.body.area}',freight='${req.body.freight}' WHERE id=${req.body.id};
  `
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '编辑失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '编辑成功',
      data: data
    })
  })
});
/* 编辑场景 */
//operate_scene
router.post('/oparate/editScene', function (req, res, next) {
  if (req.body.shelf) {
    req.body.shelf = '是'
  } else {
    req.body.shelf = '否'
  }
  let mysql = `
  UPDATE operate_scene SET title='${req.body.title}',address='${req.body.address}',shelf='${req.body.shelf}' where id=${req.body.id};
  `
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '编辑失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '编辑成功',
      data: data
    })
  })
});

/* 编辑搜索信息 */
////operate_search
router.post('/oparate/editSearch', function (req, res, next) {
  console.log(req.body)
  if (req.body.shelf) {
    req.body.shelf = '是'
  } else {
    req.body.shelf = '否'
  }
  let mysql = `UPDATE operate_search SET title='${req.body.title}',address='${req.body.address}',shelf='${req.body.shelf}' where id=${req.body.id};`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '编辑失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '编辑成功',
      data: data
    })
  })
});

//批量删除标签
router.post('/oparate/allDelLabel', function (req, res, next) {
  let mysql = `DELETE FROM operate_label WHERE `;
  for (let i = 0; i < req.body.length; i++) {
    mysql = mysql + `id=${req.body[i].id}` + ' or ';
  }
  mysql = mysql.replace(/or $/g, '');
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '批量删除标签失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '批量删除标签成功',
      data: data
    })
  })
});
//批量删除搜索
router.post('/oparate/allDelSearch', function (req, res, next) {
  let mysql = `DELETE FROM operate_search WHERE `;
  for (let i = 0; i < req.body.length; i++) {
    mysql = mysql + `id=${req.body[i].id}` + ' or ';
  }
  mysql = mysql.replace(/or $/g, '');
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '批量删除标签失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '批量删除标签成功',
      data: data
    })
  })
});

module.exports = router;